package com.stox.charting.ui.swing.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLayeredPane;
import javax.swing.OverlayLayout;

import com.stox.charting.ui.swing.model.Axis;
import com.stox.charting.ui.swing.model.Chart;
import com.stox.core.ui.swing.wrapper.JRoundPanel;

public class ChartView extends JRoundPanel{
	private static final long serialVersionUID = 6840312223979742215L;

	public static enum Type{PRICE, INDICATOR};
	
	private final List<Chart<?>> charts = new ArrayList<Chart<?>>(2);
	protected JLayeredPane contentPane = new JLayeredPane();
	private final Type type;
	private final List<Axis> axes = new ArrayList<Axis>();
	private final ChartingInternalFrameView chartingInternalFrameView;
	
	public ChartView(Type type, ChartingInternalFrameView chartingInternalFrameView) {
		super(false, new Dimension(50,50));
		this.type = type;
		this.chartingInternalFrameView = chartingInternalFrameView;
		setLayout(new BorderLayout());
		OverlayLayout layout = new OverlayLayout(contentPane);
		contentPane.setLayout(layout);
		add(contentPane, BorderLayout.CENTER);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		for(int i = 0; i < getAxesCount(); i++){
			getAxis(i).repaint();
		}
		
		for(int i = 0; i < getChartCount(); i++){
			getChart(i).repaint();
		}
	}
	
	public Type getType() {
		return type;
	}
	
	public boolean contains(Chart<?> chart){
		return charts.contains(chart);
	}
	
	public void addChart(Chart<?> chart){
		if(!charts.contains(chart)){
			charts.add(chart);
			chart.setChartView(this);
			getContentPane().add(chart, charts.size());
		}
	}
	
	public void removeChart(Chart<?> chart){
		charts.remove(chart);
		contentPane.remove(chart);
	}
	
	public Chart<?> getChart(int index){
		return charts.get(index);
	}
	
	public int getChartCount(){
		return charts.size();
	}

	public JLayeredPane getContentPane() {
		return contentPane;
	}

	public void addAxis(Axis axis){
		if(!axes.contains(axis)){
			axes.add(axis);
			add(axis, axis.getBorderLayoutConstraints());
		}
	}
	
	public int getAxesCount(){
		return axes.size();
	}

	public Axis getAxis(int index){
		return axes.get(index);
	}
	

	public ChartingInternalFrameView getChartingInternalFrameView() {
		return chartingInternalFrameView;
	}
}
